//user_server的测试客户端实现
#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <thread>
#include "etcd.hpp"
#include "channel.hpp" 
#include "utils.hpp"
#include "base.pb.h"
#include "transmit.pb.h"
//日志类参数
DEFINE_bool(log_mod,false,"true为发布模式,false为调试模式");
DEFINE_string(log_file,"","发布模式下日志存储的文件");
DEFINE_int32(log_level,0,"要输出的日志等级");
//服务注册参数
DEFINE_string(etcd_host,"http://127.0.0.1:2379","服务注册中心地址");
DEFINE_string(basedir,"/service","服务查询的根目录");
DEFINE_string(transmit_service,"/service/transmit_service","当前实例名称");
zzy_mims::ServiceChannels::ChannelPtr channel;
void string_message(const std::string &uid, const std::string &sid, const std::string &msg)
{
    //1.获取rpc服务调用句柄
    ::zzy_mims::MsgTransmitService_Stub stub(channel.get());
    //2.填写请求
    zzy_mims::NewMessageReq request;
    zzy_mims::GetTransmitTargetRsp response;
    
    request.set_request_id(zzy_mims::uuid());
    request.set_user_id(uid);
    request.set_chat_session_id(sid);
    request.mutable_message()->set_message_type(zzy_mims::MessageType::STRING);
    request.mutable_message()->mutable_string_message()->set_content(msg);

    
    //3.调用服务
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl,&request,&response,nullptr);//同步调用
    //4.验证应答是否符合预期
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(response.success());
    cout<<"msg"<<response.message().message().string_message().content()<<endl;
}
void image_message(const std::string &uid, const std::string &sid, const std::string &msg) {

    zzy_mims::MsgTransmitService_Stub stub(channel.get());
    zzy_mims::NewMessageReq req;
    zzy_mims::GetTransmitTargetRsp rsp;
    req.set_request_id(zzy_mims::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(zzy_mims::MessageType::IMAGE);
    req.mutable_message()->mutable_image_message()->set_image_content(msg);
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void speech_message(const std::string &uid, const std::string &sid, const std::string &msg) {

    zzy_mims::MsgTransmitService_Stub stub(channel.get());
    zzy_mims::NewMessageReq req;
    zzy_mims::GetTransmitTargetRsp rsp;
    req.set_request_id(zzy_mims::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(zzy_mims::MessageType::SPEECH);
    req.mutable_message()->mutable_speech_message()->set_file_contents(msg);
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void file_message(const std::string &uid, const std::string &sid, 
    const std::string &filename, const std::string &content) {

    zzy_mims::MsgTransmitService_Stub stub(channel.get());
    zzy_mims::NewMessageReq req;
    zzy_mims::GetTransmitTargetRsp rsp;
    req.set_request_id(zzy_mims::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(zzy_mims::MessageType::FILE);
    req.mutable_message()->mutable_file_message()->set_file_contents(content);
    req.mutable_message()->mutable_file_message()->set_file_name(filename);
    req.mutable_message()->mutable_file_message()->set_file_size(content.size());
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}
int main(int argc,char* argv[])
{ 
    testing::InitGoogleTest(&argc, argv);

    google::ParseCommandLineFlags(&argc,&argv,true);
    zzy_mims::init_logger(FLAGS_log_mod,FLAGS_log_file,(spdlog::level::level_enum)FLAGS_log_level);
    //关闭brpc的默认日志输出
    logging::LoggingSettings logset;
    logset.logging_dest = logging::LoggingDestination::LOG_TO_NONE;
    logging::InitLogging(logset);

    //1.创建RPC信道管理对象
    auto scm = std::make_shared<zzy_mims::ServChanManager>();
    scm->Declared(FLAGS_transmit_service);
    //2.构建服务发现对象
    auto online = std::bind(&zzy_mims::ServChanManager::ServiceOnline,scm.get(),std::placeholders::_1,std::placeholders::_2);
    auto offline = std::bind(&zzy_mims::ServChanManager::ServiceOffline,scm.get(),std::placeholders::_1,std::placeholders::_2);
    std::shared_ptr<zzy_mims::Discovery> dclient = std::make_shared<zzy_mims::Discovery>(FLAGS_etcd_host,FLAGS_basedir,online,offline); 
    //获取信道
    channel = scm->choose(FLAGS_transmit_service);
    if(!channel)
    {
        LOG_ERROR("获取用户rpc服务调用信道失败\n");
        return -1;
    }
    // string_message("6559d28e9440-0000", "会话ID1", "测试人员1发的文本消息");
    // string_message("158998af4180-0001", "会话ID1", "测试人员2发的文本消息");
    // image_message("5966d78949e3-0002", "会话ID1", "测试人员3发的图片消息");
    // speech_message("3892b567a3bb-0003", "会话ID1", "测试人员4发的语音消息");
    // file_message("6559d28e9440-0000", "158998af4180-0001", "测1向测2发的文件名称", "测1向测2发的文件数据");
    string_message("6559d28e9440-0000", "8d396a9c936d-0004", "测试人员1向测试人员2发的文本消息");
    file_message("158998af4180-0001", "1286c6aa3fe4-0006", "测2向测试人员专属群发的文件名称", "测2向测试人员专属群发的文件数据");



    return 0;
}